package com.cyk.controller;

import com.cyk.dao.LagouAuthCodeDao;
import com.cyk.pojo.LagouAuthCode;
import com.cyk.util.ToolUtil;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class EmailController {
    @Autowired
    private LagouAuthCodeDao lagouAuthCodeDao;

    /*
        发送验证码到邮箱，true成功，false失败
     */
    @GetMapping("/email/{email}/{code}")
    public Boolean email(@PathVariable String email, @PathVariable String code) {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setEmail(email);
        lagouAuthCode.setCode(code);
        lagouAuthCode.setCreatetime(new Date());
        lagouAuthCode.setExpiretime(DateUtils.addDays(new Date(), 1));
        if (lagouAuthCodeDao.save(lagouAuthCode).getId() != 0) {
            return ToolUtil.getInstance().sendEmail(email, code);
        }
        return false;
    }
}
